package com.ruoyi.common.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.service.ICommonService;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chendx
 */
@Service
public class CommServiceImpl implements ICommonService {

    private static final Logger log = LoggerFactory.getLogger(CommServiceImpl.class);

    @Autowired
    private ISysConfigService sysConfigService;

    @Override
    public String selectConfigByKey(String configKey) {
        return sysConfigService.selectConfigByKey(configKey);
    }

    @Override
    public void updateConfigByKey(String configKey, String configValue) {
        SysConfig sysConfigQuery = new SysConfig();
        sysConfigQuery.setConfigKey(configKey);
        List<SysConfig> sysConfigList = sysConfigService.selectConfigList(sysConfigQuery);
        if (CollectionUtil.isEmpty(sysConfigList) || sysConfigList.size() != 1) {
            log.info("系统配置参数{}不存在后者存在多个相同的配置!", configKey);
            return;
        }

        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigId(sysConfigList.get(0).getConfigId());
        sysConfig.setConfigValue(configValue);
        sysConfigService.updateConfig(sysConfig);
    }

}
